﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace System
{
    public static class IocManager
    {
        private static object obj = new object();

        /// <summary>
        /// 
        /// </summary>
        private static ILifetimeScope? _container { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        public static void InitContainer(ILifetimeScope container)
        {
            if (_container == null)
            {
                lock (obj)
                {
                    if (_container == null)
                    {
                        _container = container;
                    }
                }
            }
        }

        ///<summary>
        /// 获取实例
        ///</summary>
        ///<typeparam name="T"></typeparam>
        ///<returns></returns>
        public static T Resolve<T>()
        {
            return _container.Resolve<T>();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="action"></param>
        public static void BeginScope(Action<ILifetimeScope> action)
        {
            using var scope = _container.BeginLifetimeScope();
            action(scope);
        }
    }
}
